<?php 
include_once("conexion.class.php");

class User{
 //constructor	
 	var $con;
 	function User(){
 		$this->con=new DBManager;
 	}

	function insertar($campos){
		$conexion = mysql_connect("localhost", "admin", "Xurpas123");
mysql_select_db("smsnic", $conexion);
$result1= mysql_query("SELECT * FROM roles where role = '$campos[4]'", $conexion) or die(mysql_error());
if (mysql_num_rows($result1)){ 
while ($row1 = @mysql_fetch_array($result1)){
	$role = $row1['id'];
}
}
mysql_close($conexion);
		
		
		if($this->con->conectar()==true){			
			return mysql_query("INSERT INTO users (login, psw, apellido, nombre, id_role) VALUES ('".$campos[2]."', '".$campos[3]."','".$campos[1]."','".$campos[0]."','$role')");
		}
	}
	
	function actualizar($campos,$id){
		$conexion = mysql_connect("localhost", "admin", "Xurpas123");
mysql_select_db("smsnic", $conexion);
$result1= mysql_query("SELECT * FROM roles where role = '$campos[4]'", $conexion) or die(mysql_error());
if (mysql_num_rows($result1)){ 
while ($row1 = @mysql_fetch_array($result1)){
	$role = $row1['id'];
}
}
		
		mysql_close($conexion);
		if($this->con->conectar()==true){			
			return mysql_query("UPDATE users SET login = '".$campos[2]."', psw = '".$campos[3]."', apellido = '".$campos[1]."', nombre = '".$campos[0]."', id_role = '$role' WHERE id = ".$id);
		}
	}
	
	function mostrar_user($id){
		if($this->con->conectar()==true){
			return mysql_query("SELECT * FROM users WHERE id=".$id);
		}
	}

	function mostrar_users(){
		if($this->con->conectar()==true){
			return mysql_query("SELECT users.id, nombre, apellido, login, psw, role FROM users inner join roles on users.id_role=roles.id ORDER BY users.id DESC");
		}
	}

	function eliminar($id){
		if($this->con->conectar()==true){
			return mysql_query("DELETE FROM users WHERE id=".$id);
		}
	}
}
?>